package controllers

import (
	"go-scripts/common"
	"go-scripts/models"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"go-scripts/handler"
)

type ThemeController struct {
	logger *common.Log
	themeDao *mgo.Collection
}

var themeController *ThemeController

func GetThemeController() *ThemeController {
	if themeController != nil {
		return themeController
	}
	themeController = new(ThemeController)
	themeController.logger = common.GetLogger()
	var err error
	themeController.themeDao, err = models.GetModel().GetModelCollection("Theme")
	if err != nil {
		themeController.logger.Fatal(err.Error())
	}
	return themeController
}

func (t *ThemeController) GetThemes(app *handler.Global)  {
	r := app.Request
	w := app.ResponseWriter
	name := r.FormValue("name")
	theme := new(models.Theme)
	err := t.themeDao.Find(bson.M{
		"name": name,
	}).One(theme)
	if err != nil {
		logger.Error(err.Error())
		HandleError(w, r, "查询错误", 500)
		return
	}
	SendJson(w, theme)
}